/*
 * @Author: ZhangXiaodong
 * @Date: 2025-04-14 23:20:02
 * @LastEditors: ZhangXiaodong
 * @LastEditTime: 2025-04-26 00:13:25
 * @Description: 获取博客列表
 * @FilePath: /blog-ost/src/controller/blog/getList.js
 */
const mysqlPool = require('../../db/mysql/db.pool');

/**
 * 获取博客列表
 * @param {Object} query - 查询参数
 * @param {string|null} query.author - 博客作者
 * @param {string|null} query.keyword - 搜索关键词，匹配博客标题
 * @returns {Promise<Array>} - 博客列表数组
 */

const QUERY_LAYOUT = {
  author: null,
  keyword: null
}
const getList = async (query = QUERY_LAYOUT) => {
  const { author, keyword } = query;
  let sql = `
    SELECT *,
    DATE_FORMAT(createTime,'%y-%m-%d %H:%i:%s') as createTime 
    FROM blogs 
    WHERE 1=1
  `;
  const values = [];

  if (author) {
    sql += ' AND author = ?';
    values.push(author);
  }

  if (keyword) {
    sql += ' AND title LIKE ?';
    values.push(`%${keyword}%`);
  }

  sql += ' ORDER BY createTime DESC;';

  try {
    const result = await mysqlPool.exec(sql, values);
    return result;
  } catch (error) {
    console.error('获取博客列表时出错:', error);
    throw error;
  }
};

module.exports = getList;
